﻿<?php
/*
function getImage($url,$filename='') {
    if(!$url) return false;
    if(!$filename) {
    $ext=strrchr(strtolower($url),'.') ;
    if($ext!='.gif' && $ext!='.jpg' && $ext!='.png') return false ;
    $str=explode('/',$url) ;
    $filename=$str[count($str)-1] ;
    //print $filename.�\n�;
    }

    ob_start();
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $fp2=fopen($filename, 'a+') ;
    //file_put_contents($filename,$img);
    fwrite($fp2,$img);
    fclose($fp2) ;

    return $filename;
}
getImage('http://b47.photo.store.qq.com/http_imgload.cgi?/rurl4_b=35726cbf1e72b9432b4c3cb361bd1aa679d4ba56c5b3f700b030ebb1e5758dab9f9572a5a4a06642c2778d41a97a2c0ea666f07173f3b50134f7ad4260797329b9cfb5abdf683482dbf9eb07f5bf84c5d38eb847&a=35&b=47',"testpic.png");
*/

class DownloadFiles {
	protected $fileType;
	protected $fileSize;
	protected $fileUrl;
	protected $savedDir = "downloads";
	protected $savedFileName;
	protected $errors;
	protected $message;
	protected $filetestflag =0;
	public $savedfilepath;
	
	public function __construct(){
		$this->fileType[] = "*";//default can download all files;
		$this->fileSize = "*";//not used for now ;
		
	}
	
	public function setFileType($type,$add=true){
		if (!$add){
			$this->fileType = "";
		}
		if (is_array($type)&&!empty($type)){
			foreach($type as $value){
				$this->fileType[] = $value;
			}
		}else {
			$this->fileType[] = $type;
		}
	}
	
	public function setFileSize($size){
		$this->size = (float)$size;
	}
	
	public function setFileUrl($url){
		$this->fileUrl = $url;
	}
	
	public function getDefaultFileName(){
		if (strrchr($this->fileUrl,"/")){
			$url = explode("/",$this->fileUrl);
		}else {
			$url = explode("\\",$this->fileUrl);
		}
		$len = count($url)-1;
		$name = $url[$len];
		
		return $name;
	}
	
	public function getFileTypes(){
		return $this->fileType;
	}
	
	public function getFileSize(){
		return $this->fileSize;
	}
	public function setError($error){
		$this->errors[] = $error;
	}
	public function getErrors(){
		return $this->errors;
	}
	
	public function getMessage(){
		return $this->message;
	}
	
	public function getImages($sring){
		if (preg_match("/<img.*src=[\"\']?(.*)[\"\']?\/>/",$matches)){
			return $matches;
		}else {
			return false;
		}
		
	}
	
	public function filetest($filepath){
		if(file_exists($filepath)){
			
			$this->filetestflag +=1;
			$filearr = explode(".",$filepath);
			$count = count($filearr);
			$last = $count-1;
			for ($i=0;$i<$last;$i++){
				$temarr[] = $filearr[$i];
			}
			$fileprex = implode("/",$temarr);
			
			if (strrchr($fileprex,"(")){
				$fileprexarr = explode("(",$fileprex);
				$fileprex = $fileprexarr[0];
			}
			$filepath = $fileprex."(".$this->filetestflag.")".".".$filearr[$last];
			
			$this->filetest($filepath);
		}else {
			//echo $filepath." last <br />";
			$this->savedfilepath = $filepath;
			return $filepath;
		}
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $fileUrl;
	 * @param string $filename saved file name;
	 * @return unknown
	 */
	public function doDownloadFiles($fileUrl,$filename=""){
		$this->setFileUrl($fileUrl);
		$type = substr(strrchr($fileUrl,"."),1);
		if (!in_array($type,$this->fileType)&&!in_array("*",$this->fileType)){
			$this->errors[] = "file type not allowed.";
			//$this->message["error"] .= $this->errors[];
			return $this;
		}
		$directory = $this->savedDir;
		if (!is_dir($directory)){
					mkdir($directory);
		}
		if(!$filename){
			$filename = $directory."/".$this->getDefaultFileName();
		}else{
			if (strrchr($filename,"/")){
				$lasttemp ="";
				$dirs = explode("/",$filename);
				
				
				$dircount = count($dirs);
				
				if (strrchr($filename,"/")=="/"){
					$lasttemp = $this->getDefaultFileName();
					
				}else{
					$dircount = $dircount-1;
					$lasttemp = "/".$dirs[$dircount];
				}
		
				for($i=0;$i<$dircount;$i++){
					$dir = $dirs[$i];
					if (!is_dir($directory."/".$dir)){
						mkdir($directory."/".$dir);	
					}
					$directory.= "/".$dir;
					
				}
				$filename = $directory.$lasttemp;
			}
		}
		
		$filename = $this->filetest($filename);
		
		if (!$filename){
			$filename = $this->savedfilepath;
			
		}
		
		if(strlen(strrchr($filename,"."))>5||!strrchr($filename,".")){
		
			$filename .=".jpg";
		}
		
		ob_start();
	    readfile($fileUrl);
	    $img = ob_get_contents();
	    ob_end_clean();
	    $fp = fopen($filename, 'a') ;
	    if ($fp){
	    	fwrite($fp,$img);
	    	fclose($fp);
	    	$this->message["log"] = "success download file from $fileUrl;<br />saved at : $filename";
	    	$this->message["url"] = $filename;
	    }else{
	    	$this->errors[] = "failed open file ".$filename;
	    }
	    //file_put_contents($filename,$img);
	    $this->message["errors"] = $this->errors;
	    return $this;
	}
	
}

$url ="http://www.w3school.com.cn/i/site_bg.gif";
$url ='http://www.google.com.hk/intl/zh-CN/images/logo_cn.png';
$url = 'http://www.phpletter.com/images/loading.gif';
$url = 'http://tx.bdimg.com/sys/portraitb/item/30f3c5b5bbc3e7f78e14.jpg';
$url = 'http://tx.bdimg.com/sys/portraitb/item/c9473838343132313231e802.jpg';
$url = 'http://tx.bdimg.com/sys/portraitb/item/b500d5d2c7aec3a8d814.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/2ba0b1afc9cba874a1eec2e4c0e1a814.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/9901d1aaa4cea5cea5a8a5f3a5c9d114.jpg';
$url = 'http://tx.bdimg.com/sys/portraitb/item/e450d2bbcff4d0a1c1d5d614.jpg';
$url = 'http://tx.bdimg.com/sys/portraitb/item/cd21becdcac7c7efc4c8d414.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/86fb6c79633734383131301b06.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/af33bec5ceb2bafca1a4d7bcd0c2c4ef820f.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/5819c4a7c4a7c8cbb6f94913.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/779a4e414e41d1f23411.jpg';
$url ='http://hiphotos.baidu.com/vo%D1%A9/pic/item/36cfaffd3b31f00c242df252.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/5049c4e3c8c3c3cecfebd3d0b3e1b0f2f309.jpg';
$url ='http://b47.photo.store.qq.com/http_imgload.cgi?/rurl4_b=35726cbf1e72b9432b4c3cb361bd1aa679d4ba56c5b3f700b030ebb1e5758dab9f9572a5a4a06642c2778d41a97a2c0ea666f07173f3b50134f7ad4260797329b9cfb5abdf683482dbf9eb07f5bf84c5d38eb847&a=35&b=47';
$url ='http://tx.bdimg.com/sys/portraitb/item/2f3c687866313032372d12.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/81d842656c6c61b0aeb5adc4ad370e.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/ac8e6d697373796f7535323078ee05.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/9f2467666476686276d40f.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/5d2b436f6f6c5fbde05413.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/254f6d696b65793435303830373935373c0f.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/6fa4d348d3487a68753812.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/2f48b0cdcef7d5bedc14.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/5ce1d1a9baaecee85508.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/e84ab0d9b1e4d0a1ced7c5ae3132335608.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/370c746377746377636e5608.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/dad6c0b6373735385508.jpg';
$url ='http://tx.bdimg.com/sys/portraitb/item/6fa4d348d3487a68753812.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/cca743e96e5ed07f2cf534f7.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/e4fc9cfc01ed4732024f5636.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/b0eced2e8db50aa898250af6.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/cca743e96e5ed07f2cf534f7.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/b0eced2e8db50aa898250af6.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/913b70810d28956866096ef5.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/68cbc837cd1a52cfa8018ef4.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/8ee84a1165c74507f2de32eb.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/c278711913ef1749403417ea.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/11fc0efe8f128a0f242df2ee.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/20aaf22a8f1dc262359bf7ed.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/b7f04688b93901ae533d9240.jpg';
$url ='http://hiphotos.baidu.com/%C7%D7%C7%D7zhu/pic/item/f7b694ae9af315ac1c17a245.jpg';
$url ='http://b19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=a04d06a6ae096e5ce771f296b8fc14694891a7cb77df9f4017bd4568fc34bfa1cf254f9061876a1f59d7c25514e924db66e97b237211c935ebac557c635d17b11c7d379344a335907f20af1ecb1b20dfef3abb73&a=17&b=19';
$url ='http://b19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=a04d06a6ae096e5ce771f296b8fc14694891a7cb77df9f4017bd4568fc34bfa1cf254f9061876a1f59d7c25514e924db66e97b237211c935ebac557c635d17b11c7d379344a335907f20af1ecb1b20dfef3abb73&a=17&b=19';
$url ='http://b16.photo.store.qq.com/http_imgload.cgi?/rurl4_b=a04d06a6ae096e5ce771f296b8fc1469287769df2f74b469daa6041413b0c37183ddc4ea438db84ba230dfc99a4198f2d1efa573d8fc9b2ac3790898dd1a9a00aab53485ef4608b1e3ae84005f0f9e9499085eb6&a=16&b=16';
$url ='http://b16.photo.store.qq.com/http_imgload.cgi?/rurl4_b=a04d06a6ae096e5ce771f296b8fc1469b371b55f55fbb875854d1ca491763b822d52c8add872db8526c753fd44d316c7ad7488e1e300aeed096b0c7717b8ec4bb7f5293aee28b64960166a47dbed1797b345860f&a=9&b=16';
$url ='http://b19.photo.store.qq.com/http_imgload.cgi?/rurl4_b=a04d06a6ae096e5ce771f296b8fc1469915f7773e79fee73c19ccee4eedfb5d807315b7afd0af7b8116766282ed3f5ade023cd8c5163953c5261f3e68d26f76472aac81cb4a118fb7c393a42a333fce282c6a460&a=19&b=19';
$url ='http://b8.photo.store.qq.com/http_imgload.cgi?/rurl4_b=a04d06a6ae096e5ce771f296b8fc14697761dc32bcee24c2ada81880835368b89e3269c791d35306b70b223b36b3659c478f567af8bd1ee68381845e9771c26c84e85ecac20ecbf2d49eaab1649fd32d15913a21&a=19&b=8';
$url ='http://b65.photo.store.qq.com/http_imgload.cgi?/rurl4_b=cf187b8d6ae2861c3c9b4a68a052864d262b32dd97564174001cf3a59412c18fa67801ddf51e94a07f706b300cc40b7f45e5ff3601f482dafa21d741680d9b37ffb09eb0c00af762ae1bc3a4ed155be36b4c8d9f&a=62&b=65';
$url ='http://b70.photo.store.qq.com/http_imgload.cgi?/rurl4_b=cf187b8d6ae2861c3c9b4a68a052864dae62c57eae3f2d801fc64e12cd9e41836fa75811171348520ae56399f757d8bb3274cd722a209b1cb6c7f14bcf9255d9a0e9bdc27387a40208b4b8e1797209857e53984b&a=67&b=70';
$url ='http://b70.photo.store.qq.com/http_imgload.cgi?/rurl4_b=cf187b8d6ae2861c3c9b4a68a052864d6eb2a67800245b4ffc7fbbf45ca1bed6df13ed88f9372ce338dc8338b9e0a29ba3d92482c3093ee5992e0557f2979a9dfb743b79c0fecca99da60e8c0d42186996ba31bb&a=62&b=70';
$url ='http://b67.photo.store.qq.com/http_imgload.cgi?/rurl4_b=cf187b8d6ae2861c3c9b4a68a052864d0d1bb147ce4cf36180cc287579fa3faf5c828b0bed6b57fadcb9c92d151087b5f302b8a715d15b7304b4e83a686e6e81f2ef0130884d66ef7789bb78b5743b4c65986900&a=67&b=67';
$url ='http://b28.photo.store.qq.com/http_imgload.cgi?/rurl4_b=cf187b8d6ae2861c3c9b4a68a052864d604f9217d2a4d15dec037cd65af1a92d55825769b80962cb7e475e6d34e95bdb9dcdade166cae1c157628b7a340025d6261a1a325b50e87c581a925252ae3a7f87054ff9&a=28&b=28';
$url ='http://b25.photo.store.qq.com/http_imgload.cgi?/rurl4_b=cf187b8d6ae2861c3c9b4a68a052864d3ae568e8d2bcdc4130342f3e39dc23788701a0aa11e1b07c34013a54b75fe6175ed8c479728fc5d7bd32565bafc7969b94a36a7a4756d64fd9e006125dc1b5a22a1acd2b&a=24&b=25';
$url ='http://b5.photo.store.qq.com/http_imgload.cgi?/rurl4_b=bdbd9a578c2d74053fbd31e1ccdb35155011401c72f4a6132d165782424c4e56ccdefd81ecf7e560de99578ad29b1397653a938b272c91408d4d9795855bed2701bdd1b89036d51495f90d48c302544285fcd0bd&a=5&b=5';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/a3416664e182269a8db10d80.jpg';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/c8ba41cf766c8a5abe09e680.jpg';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/b89ff41f5a3c85bfaf513380.jpg';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/e9734989e95d4157b31bba80.jpg';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/ce28f6bb8fa8f84f19d81f87.jpg';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/1687aa56ef0b0a4b0cf3e387.jpg';
$url ='http://hiphotos.baidu.com/qinqin9264/pic/item/06a8863aca53d7c7838b137c.jpg';
$url ='http://hiphotos.baidu.com/honglingbb/pic/item/363f1f1b66df5e8da78669b4.jpg';
$url ='http://fmn.rrfmn.com/fmn054/20110331/2030/p_large_pyQK_663c000200875c42.jpg';
$url ='http://fmn.rrfmn.com/fmn055/20110330/2020/p_large_bNkb_0ba80000439d5c16.jpg';
$url ='http://fmn.rrfmn.com/fmn055/20110330/1535/p_large_RJJF_561200084c035c44.jpg';
$url ='http://fmn.rrfmn.com/fmn048/20110330/1420/p_large_WEq6_20f9000a70525c44.jpg';
$url ='http://fmn.rrfmn.com/fmn051/20110401/2300/p_large_ZlxV_474000003b815c15.jpg';



$urlstring = '<img src="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png" alt="http://www.google.com.hk/intl/zh-CN/images/logo_cn.png">';

$savedFilename = "";
$typearr = array("png","jpg","jpeg","gif");
$download = new DownloadFiles();
$download->setFileType($typearr,true);
$download->doDownloadFiles($url,$savedFilename)->getMessage();
$message = $download->getMessage();
$url = $message["url"];
?>
<html>
<head>
<title><?php echo $url;?></title>
</head>
<body>
<?php 
echo "<img src='$url' title='$url' alt='$url' />";
var_dump($message);
?>
</body>
</html>

 